package com.xxl.job.extend.controller;

import com.xxl.job.admin.core.model.XxlJobGroup;
import com.xxl.job.admin.dao.XxlJobGroupDao;
import com.xxl.job.admin.dao.XxlJobInfoDao;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.util.GsonTool;
import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.admin.core.model.XxlJobInfo;
import com.xxl.job.admin.service.XxlJobService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/api/extend")
public class JobApiExtendController {

    @Resource
    private XxlJobService xxlJobService;
    @Resource
    private XxlJobInfoDao xxlJobInfoDao;
    @Resource
    private XxlJobGroupDao xxlJobGroupDao;

    @RequestMapping("/{uri}")
    @ResponseBody
    @PermissionLimit(limit=false)
    public ReturnT<String> api(HttpServletRequest request, @PathVariable("uri") String uri, @RequestBody(required = false) String data) {
        // valid
        if (!"POST".equalsIgnoreCase(request.getMethod())) {
            return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, HttpMethod not support.");
        }
        if ("add".equals(uri)) {
            XxlJobInfo xxlJobParam = GsonTool.fromJson(data, XxlJobInfo.class);
            return xxlJobService.add(xxlJobParam);
        } else if ("update".equals(uri)) {
            XxlJobInfo xxlJobParam = GsonTool.fromJson(data, XxlJobInfo.class);
            return xxlJobService.update(xxlJobParam);
        } else if ("remove".equals(uri)) {
            int id = Integer.parseInt(data);
            return xxlJobService.remove(id);
        } else if ("start".equals(uri)) {
            int id = Integer.parseInt(data);
            return xxlJobService.start(id);
        } else if ("stop".equals(uri)) {
            int id = Integer.parseInt(data);
            return xxlJobService.start(id);
        } else if ("validate".equals(uri)) {        //判断jobId是否存在
            int id = Integer.parseInt(data);
            XxlJobInfo xxlJobInfo = xxlJobInfoDao.loadById(id);
            if (xxlJobInfo == null) {
                return new ReturnT<String>(ReturnT.FAIL_CODE, "jobId为" + id + "的job不存在");
            }
            return ReturnT.SUCCESS;
        } else if ("getGroupId".equals(uri)) {
            String appName = data.replace("\"","");
            List<XxlJobGroup> xxlJobGroups = xxlJobGroupDao.pageList(0, 10, appName, "");
            if(StringUtils.isEmpty(xxlJobGroups)) {
                return new ReturnT<String>(ReturnT.FAIL_CODE, "app_name为" + appName + "的group不存在");
            }
            ReturnT<String> success = ReturnT.SUCCESS;
            success.setMsg(xxlJobGroups.get(0).getId()+"");
            return success;
        } else {
            return new ReturnT<String>(ReturnT.FAIL_CODE, "invalid request, uri-mapping("+ uri +") not found.");
        }
    }

}
